package kr.hkit.androidcomplete02;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends ExpandableListActivity {

	private String[] mainTitles;
	private String[][] subTitles;
	private String[] pkgLists;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mainTitles = getResources().getStringArray(R.array.titles); //배열가져옴
		pkgLists = getResources().getStringArray(R.array.pkglist);
		subTitles = new String[mainTitles.length][];
		
		for (int i=0; i<mainTitles.length; i++){
			//이름에 해당하는 리소스아이디를 찾아서 넘겨줌. 이름,데이터타입,현재패키지명
			int resId = getResources().getIdentifier("subtitles"+i, "array", getPackageName());
			subTitles[i] = getResources().getStringArray(resId); 
		}
		
		/*//검증용
		for(int i=0; i<mainTitles.length; i++){
			Log.d("Test", mainTitles[i]);
			
			for(int j=0; j<subTitles[i].length; j++){
				Log.d("   Test", subTitles[i][j]);
			}
		}*/
		
		
		MyExpandableListAdapter adapter = new MyExpandableListAdapter();
		setListAdapter(adapter);
		
		
	} //네비게이션드로우. 왼쪽에서 메뉴바 나왔다가 사라지는거
	//액션바에서 스와이프?????? 프로젝트 첨에 만들때 지정하면 쉽게됨
	
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		 super.onChildClick(parent, v, groupPosition, childPosition, id);
		 //클릭시 실행시키는거
		 
		 Intent intent = new Intent();
		 //package kr.hkit.androidcomplete0.fragment.FragmentEx
		 intent.setClassName(getPackageName(), getPackageName()+"."+pkgLists[groupPosition]+"."+subTitles[groupPosition][childPosition]);
		 //혹은 setComponent()
		 
		 startActivity(intent);
		 
		 return true;
	}

	
	class MyExpandableListAdapter extends BaseExpandableListAdapter{
		
		@Override
		public int getGroupCount() { //title 갯수
			return mainTitles.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) { //그룹포지션에 해당하는 길이
			return subTitles[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mainTitles[groupPosition];
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return subTitles[groupPosition][childPosition];
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() { //배열이면 true 안바뀐다. 컬렉션쓰면 false
			return true;
		}

		@Override //코드이용
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) { //그룹뷰 어떻게 보여줄것인가.
			//우리가 꾸미지 않는 이상은 textView
			TextView tv = new TextView(MainActivity.this);
			tv.setTextSize(25);
			tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.START);
			tv.setPadding(36, 0, 0, 0);
			tv.setText(getGroup(groupPosition).toString()); //getGroup가 object형임
			return tv;
		}

		@Override //레이아웃 인플레이터 이용
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			TextView tv = (TextView) inflater.inflate(android.R.layout.simple_expandable_list_item_1, null);
			tv.setText(getChild(groupPosition, childPosition).toString());
			return tv;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}
}
